package com.example.demoweb.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.example.demoweb.constant.FtpProperties;
import com.example.demoweb.entity.Attachment;
import com.example.demoweb.framework.ExecuteResult;
import com.example.demoweb.service.IAttachmentService;
@Controller
@RequestMapping(value="${apiPath}/attachment")
public class AttachmentController extends BaseController {

	@Autowired
	private IAttachmentService attachmentService;
	
	/**
	   * @param request
	   * @param response
	   * @param model
	   * @return String
	   */
	  @RequestMapping(value = "/online_yasuo")
	  @ResponseBody
	  public ExecuteResult<Object> online_yasuo(HttpServletRequest request, HttpServletResponse response,
			  @RequestParam(value="imgval") String imgval,Model model
			  ,@RequestParam(value="id") String id
			  ,@RequestParam(value="idcard") String idcard
			  ,@RequestParam(value="name") String name){
	    ExecuteResult<Object> executeResult = new ExecuteResult<Object>();
	    try {
	      if (imgval!=null&&imgval.length()>0) {
	    	  Attachment attachment = new Attachment();
	    	  attachment.setRelaId(idcard);
	    	  attachment.setAttType(id);
	    	  attachment.setActiveFlage("1");
	    	  Attachment byModel = attachmentService.getByModel(attachment);
	        /*String newname=UUIDToll.getUUID();
	        attachment = Base64Image.GenerateImage(imgval, "bzgzpt/aba",id,newname,idcard,name);*/
	        if(byModel!=null){
	        	byModel.setActiveFlage("0");
	        	attachmentService.update(byModel);//删除附件
	        }
	        attachmentService.save(attachment);
	        executeResult.setSuccess(true);
	      }else{
	    	  executeResult.setSuccess(false);
	      }
	    } catch (Exception e) {
	    	executeResult.setError(e.getMessage());
	    	//executeResult.setSuccess(false);
	    }
	    return executeResult;
	  }
	/**
	 * 单文件上传
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/saveOne")
	public ExecuteResult<String> save(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response, Model model,@RequestParam(value = "relaId")String relaId) {
		ExecuteResult<String> result = new ExecuteResult<String>();
		result = attachmentService.saveOne(file,relaId);
		return result;
	}
	/**
	 * 多文件上传
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	/*@RequestMapping(value = "/moreuploadAttachment")
	public String moreupload(@RequestParam(value = "fileC", required = false) List<MultipartFile> fileC,
			HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		Attachment attachment = new Attachment();
		Aba31 aba31 = new Aba31();
		for (int i = 0; i < fileC.size(); i++) {
			if (!fileC.get(i).isEmpty()) {
				attachment=FileupdownUtil.saveAttachment(fileC.get(i), request,aba31.getAbax3101().toString(), aba31.getAbax0118(), "shopping");
			}
		}
		attachmentService.save(attachment);
		return "module/uap/test";
	}*/
	
	 /**
	  * summernote富文本编辑器上传图片，返回路径 
	  * @param request
	  * @param response
	  * @param imgval
	  * @param model
	  * @param attType
	  * @param name
	  * @return
	  */
	 /*@RequestMapping(value = "/summernoteUpload")
	  @ResponseBody
	  public ExecuteResult<Object> summernoteUpload(HttpServletRequest request, HttpServletResponse response,Model model,
			  @RequestParam(value="imgval") String imgval,
			  @RequestParam(value="attType") String attType,
			  @RequestParam(value="name") String name){
	    ExecuteResult<Object> executeResult = new ExecuteResult<Object>();
	    try {
	      if (imgval!=null&&imgval.length()>0) {
	    	  Attachment attachment = new Attachment();
	        String newname=UUIDToll.getUUID();
	        attachment = Base64Image.GenerateImage(imgval, "bzgzpt/summernote",attType,newname,"",name);
	        attachmentService.save(attachment);
	        executeResult.setSuccess(true);
	        executeResult.setData(attachment.getFileUrl());
	      }else{
	    	  executeResult.setSuccess(false);
	      }
	    } catch (Exception e) {
	    	executeResult.setError(e.getMessage());
	    	//executeResult.setSuccess(false);
	    }
	    return executeResult;
	  }*/
}
